package com.spzx.product.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rabbitmq.client.Channel;
import com.spzx.order.domain.OrderInfo;
import com.spzx.product.service.SkuStockService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class SpzxProductMqListener {
    /*
     rabbitmqService.sendMsg("spzx.product.stock.exchange",
                        "product.stock.release.msg" , orderNo )
     */
    @Resource
    SkuStockService skuStockService;


    //支付成功 发送消息： 更新销量和库存
    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange = @Exchange(value="spzx.payment.exchange",
                            type = ExchangeTypes.TOPIC),
                    value = @Queue("spzx.product.payment.queue"),
                    key = "spzx.pay.success.msg"
            )
    })
    public void updateSalesAndStock(Message message , Map<String,String> map , Channel channel) throws IOException {
        try {
            String orderNo = map.get("orderNo");
            String type = map.get("type");
            if("1".equals(type)){
                //支付成功：  加销量  扣减库存
                skuStockService.updateStockAndSales(orderNo);
            }else{
                //支付失败/取消
                //释放锁定的库存
                skuStockService.releaseStock(orderNo);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            if(message.getMessageProperties().isRedelivered()){
                //日志记录丢弃的消息
                log.error("spzx-product更新库存和销量失败,消息内容:{}" , JSON.toJSONString(map));
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            }else{
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
        }
    }




    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange = @Exchange(value="spzx.product.stock.exchange",
                            type = ExchangeTypes.TOPIC),
                    value = @Queue("spzx.product.stock.release.queue"), //释放订单锁定的库存
                    key = "product.stock.release.msg"
            )
    })
    public void releaseStock(Message message , String orderNo , Channel channel) throws IOException {
        try {
            //调用释放库存的业务方法
            skuStockService.releaseStock(orderNo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            if(message.getMessageProperties().isRedelivered()){
                //日志记录丢弃的消息
                log.error("spzx-product释放库存失败,消息内容:{}" , JSON.toJSONString(orderNo));
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            }else{
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
        }
    }
}
